<template>
<div class="home" ref="home" @scroll='top = $event.srcElement.scrollTop'>
  <van-pull-refresh v-model="refreshing" @refresh="onRefresh">

    <!-- 基本的提示信息 start -->
    <van-notice-bar left-icon="volume-o" :scrollable="false" mode="closeable">
      <van-swipe
        vertical
        class="notice-swipe"
        :autoplay="3000"
        :show-indicators="false"
      >
        <van-swipe-item>内容 1</van-swipe-item>
        <van-swipe-item>内容 2</van-swipe-item>
        <van-swipe-item>内容 3</van-swipe-item>
      </van-swipe>
    </van-notice-bar>
    <!-- 基本的提示信息 end -->

    <!-- 搜索框 start -->
    <div class="header-search">
      <van-search
        v-model="keyword"
        show-action
        placeholder="请输入搜索关键词"
        background="#c82519"
        class="search"
        disabled
      >
        <!-- <template #left-icon>
          <img :src="$img.zk" alt="">
        </template> -->
        <template #action>
          <div class="loginbtn">登录</div>
        </template>
      </van-search>
    </div>
    <!-- 搜索框 end -->

    <!-- 轮播图 start -->
    <van-swipe :autoplay="3000" class="v-swipe">
      <van-swipe-item v-for="(item, index) in images" :key="index">
        <img v-lazy="imgBaseUrl + item.img_url" />
      </van-swipe-item>
    </van-swipe>
    <!-- 轮播图 end -->

    <!-- 详情页 start -->
    <div class="details">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :immediate-check='false'
      >
        <div class="d-list" v-for="i in goodsLen" :key="i"> 
          <Detail :goods='goods[2*i - 2] || {}'></Detail>
          <Detail :goods='goods[2*i - 1] || {}'></Detail>
        </div>
      </van-list>
    </div>
    <!-- 详情页 end -->
    
    <Tabbar></Tabbar>
  </van-pull-refresh>
</div>
</template>

<script>
import {
  Tabbar,
  Detail
} from '@/components'

export default {
  name: '',
  components: {
    Tabbar,
    Detail
  },

  computed: {
    // 用于处理商品数据，商品数据是长度为2
    goodsLen () {
      return Math.floor(this.goods.length / 2)
    },

    imgBaseUrl () {
      return this.$img.imgServe.substring(0, this.$img.imgServe.lastIndexOf('/'))
    }
  },

  data() {
    return {
      goods: [],
      keyword: '',
      page: 1,
      images: [],
      refreshing: false, // 在下拉过程中它是true，当它是true时无法进行第二次下拉
      loading: false, // 当滚动条抵达小于offset时它是true，当它是true时无法进行第二次触底
      finished: false, // 当后端返回的商品列表为空，没有数据时
      top: 0,
    }
  },

  methods: {
    init () {
      const params = {
        size: 10,
        page: this.page
      }
      this.$api.getGoodList(params).then(res => {
        // 如果当前数据恢复到第一页时，页面重新刷新，下拉过程需要改变状态，完成也需要改变状态
        if (this.page === 1) {
          this.goods = res.list
          this.refreshing = false
          this.finished = false
        } else {
          this.goods = [...this.goods, ...res.list]
          this.loading = false
          
          if (this.goods.length >= res.total) {
            this.finished = true
          }
        }
      })
    },

    // 下拉刷新触发时
    onRefresh () {
      this.page = 1
      this.init()
    },
    
    onLoad() {
      this.page++
      this.init()
    }
  },

  mounted () {
    this.init()
    this.$api.getBanner({}).then(res => {
      console.log( res )
      this.images = res
      console.log( this.images )
    })
  },

  activated () {
    // 当页面激活时，自动获取上一次的位置
    this.$refs.home.scrollTop = this.top
  }
}
</script>

<style lang="scss" scoped>
  .home {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    overflow: auto;
  }

  .notice-swipe {
    height: .533333rem;
    line-height: .533333rem;
  }

  .van-search {
    border-radius: .266667rem;
    .loginbtn {
      color: #fff;
    }
  }

  .v-swipe {
    width: 10rem;
    height: 3.733333rem;
    margin-top: .133333rem;
    .van-swipe-item {
      width: 9.333333rem;
      height: 3.733333rem;
      img {
        width: 9.333333rem;
        height: 3.733333rem;
        margin: 0 .333333rem;
        border-radius: .266667rem;
      }
    }
  }

  .details {
    width: 10rem;
    overflow: hidden;
    margin: .133333rem 0;
    padding-bottom: 1.6rem;
    .d-list {
      margin: .266667rem 0;
      width: 10rem;
      height: 6.933333rem;
      display: flex;
      justify-content: space-around;
    }
  }

</style>